package com.iexbuy.ihg.adapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.Shop;
;

public class ShopsAdapter extends BaseAdapter {

	private List<Shop> datas = new ArrayList<Shop>();
	private Context context;

	public ShopsAdapter(Context context, List<Shop> datas) {
		super();
		this.context = context;
		if (datas != null) this.datas = datas;
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int location) {
		return datas.get(location);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.fragment_home_list_item, null);
		}
		Shop vo = (Shop)datas.get(position);

		((TextView) convertView.findViewById(R.id.listTitle)).setText(vo.getBusinessName());
		((TextView) convertView.findViewById(R.id.listSubTitle)).setText(vo.getBusinessName());
		if(vo.getDistance()!=null||vo.getRank()!=null||vo.getRankPeopleNumber()!=null||vo.getThumbnailUrl()!=null){
			((TextView) convertView.findViewById(R.id.duration)).setText(CommUtil.formatDistance(vo.getDistance()));

			((TextView) convertView.findViewById(R.id.point)).setText(new BigDecimal(vo.getRank()).setScale(1, BigDecimal.ROUND_HALF_UP).toString() + "分(" + vo.getRankPeopleNumber() + "人)");
			App.bitmapUtils.display(((ImageView) convertView.findViewById(R.id.listIcon)), vo.getThumbnailUrl());
		}
		return convertView;
	}
	
	private Double getMagnitude(String magnitudeStr) {
		Double magnitude = Double.valueOf("0");
		try {
			magnitude = Double.valueOf(magnitudeStr);
		} catch (Exception e) {
		}
		return magnitude;
	}
	
}
